package org.shzhyun.tencent;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.Region;
import com.tencentcloudapi.tts.v20190823.TtsClient;
import com.tencentcloudapi.tts.v20190823.models.TextToVoiceRequest;
import com.tencentcloudapi.tts.v20190823.models.TextToVoiceResponse;
import lombok.extern.slf4j.Slf4j;
import org.shzhyun.TtsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;

@Slf4j
@Component
public class TtsComponent implements TtsService {

    @Value("${tts.filePath}")
    private String ttsfilePath;
//    public static void main(String[] args){
//        //SecretId:AKIDNjsodIl01pSzIPJ3c2LfttgGco0zkIdl
//        //SecretKey:3vZ3PbbOyLWUEl6FBwVt44tVv9C4kDUY
//        Credential cred = new Credential("AKIDNjsodIl01pSzIPJ3c2LfttgGco0zkIdl", "3vZ3PbbOyLWUEl6FBwVt44tVv9C4kDUY");
//        TtsClient ttsClient = new TtsClient(cred, Region.Shanghai.getValue());
//        TextToVoiceRequest req = new TextToVoiceRequest();
//        req.setText("啤酒饮料八宝粥,瓜子泡面花生米。");
//        req.setSessionId(System.currentTimeMillis()+"");
//        try {
//            TextToVoiceResponse textToVoiceResponse = ttsClient.TextToVoice(req);
//            byte[] decode = Base64.getDecoder().decode(textToVoiceResponse.getAudio());
//            try {
//                FileOutputStream fileOutputStream = new FileOutputStream("C:/gds/mpframeworkbackend/org.shzhyun.tts/ttsfile/output/"+System.currentTimeMillis()+".wav");
//                fileOutputStream.write(decode);
//                fileOutputStream.close();
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//            System.out.println(textToVoiceResponse.getAudio());
//        } catch (TencentCloudSDKException e) {
//            throw new RuntimeException(e);
//        }
//    }

    @Override
    public String doTranse(String text){
        //SecretId:AKIDNjsodIl01pSzIPJ3c2LfttgGco0zkIdl
        //SecretKey:3vZ3PbbOyLWUEl6FBwVt44tVv9C4kDUY
        Credential cred = new Credential("AKIDNjsodIl01pSzIPJ3c2LfttgGco0zkIdl", "3vZ3PbbOyLWUEl6FBwVt44tVv9C4kDUY");
        TtsClient ttsClient = new TtsClient(cred, Region.Shanghai.getValue());
        TextToVoiceRequest req = new TextToVoiceRequest();
        req.setText(text);
        req.setSessionId(System.currentTimeMillis()+"");
        //音色列表:  https://cloud.tencent.com/document/product/1073/92668
        req.setVoiceType(1008L);
        String name = System.currentTimeMillis()+"";
        try {
            TextToVoiceResponse textToVoiceResponse = ttsClient.TextToVoice(req);
            byte[] decode = Base64.getDecoder().decode(textToVoiceResponse.getAudio());
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(ttsfilePath+"/output/"+name+".wav");
                fileOutputStream.write(decode);
                fileOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            System.out.println(textToVoiceResponse.getAudio());
        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }
        return name;
    }
}
